import { ref, onMounted } from 'vue'
import {
  getCreatorByManage,
  getGroupByManage,
  getCreatorList,
  getGroupList,
  getPlanMaps
} from '@/api/templateManage'

export function useDict(type) {
  const creatorList = ref([])
  const groupList = ref([])
  const planList = ref([])

  onMounted(() => {
    init()
  })
  const init = () => {
    if (type === 0) {
      const obj = {
        label: '全部',
        value: ''
      }
      creatorList.value = []
      getCreatorByManage().then((res) => {
        if (res.data && res.data.length > 0) {
          creatorList.value = res.data.map((v) => {
            return {
              label: v.userName,
              value: v.creator
            }
          })
        }
        creatorList.value.unshift(obj)
      })
      groupList.value = []
      const objManage = {
        label: '全选',
        value: ''
      }
      getGroupByManage().then((res) => {
        if (res.data && res.data.length > 0) {
          groupList.value = res.data.map((v) => {
            return {
              label: v,
              value: v
            }
          })
        }
        groupList.value.unshift(objManage)
      })
    } else if (type === 1 || type === 2 || type === 4) {
      const obj = {
        label: '全部',
        value: ''
      }
      getCreatorList(type).then((res) => {
        if (res.data && res.data.length > 0) {
          creatorList.value = res.data.map((v) => {
            return {
              label: v.userName,
              value: v.creator
            }
          })
        }
        creatorList.value.unshift(obj)
      })
      const objGroupList = {
        label: '全选',
        value: ''
      }
      getGroupList(type).then((res) => {
        if (res.data && res.data.length > 0) {
          groupList.value = res.data.map((v) => {
            return {
              label: v,
              value: v
            }
          })
        }
        groupList.value.unshift(objGroupList)
      })
      getPlanMaps(type).then((res) => {
        if (res.data && res.data.length > 0) {
          planList.value = res.data.map((v) => {
            return {
              label: v,
              value: v
            }
          })
        }
        planList.value.unshift(objGroupList)
      })
    }
  }
  return { creatorList, groupList, planList }
}

export function useDictTwo(type) {
  const creatorList = ref([])
  const groupList = ref([])
  const planList = ref([])

  if (type === 0) {
    const obj = {
      label: '全部',
      value: ''
    }
    creatorList.value = []
    getCreatorByManage().then((res) => {
      if (res.data && res.data.length > 0) {
        creatorList.value = res.data.map((v) => {
          return {
            label: v.userName,
            value: v.creator
          }
        })
      }
      creatorList.value.unshift(obj)
    })
    groupList.value = []
    const objManage = {
      label: '全选',
      value: ''
    }
    getGroupByManage().then((res) => {
      if (res.data && res.data.length > 0) {
        groupList.value = res.data.map((v) => {
          return {
            label: v,
            value: v
          }
        })
      }
      groupList.value.unshift(objManage)
    })
  } else if (type === 1 || type === 2 || type === 4) {
    const obj = {
      label: '全部',
      value: ''
    }
    getCreatorList(type).then((res) => {
      if (res.data && res.data.length > 0) {
        creatorList.value = res.data.map((v) => {
          return {
            label: v.userName,
            value: v.creator
          }
        })
      }
      creatorList.value.unshift(obj)
    })
    const objGroupList = {
      label: '全选',
      value: ''
    }
    getGroupList(type).then((res) => {
      if (res.data && res.data.length > 0) {
        groupList.value = res.data.map((v) => {
          return {
            label: v,
            value: v
          }
        })
      }
      groupList.value.unshift(objGroupList)
    })
    getPlanMaps(type).then((res) => {
      if (res.data && res.data.length > 0) {
        planList.value = res.data.map((v) => {
          return {
            label: v,
            value: v
          }
        })
      }
      planList.value.unshift(objGroupList)
    })
  }
  return { creatorList, groupList, planList }
}
// defineExpose({
// 	init
// })
